#include "perimeter.h"

Perimeter::Perimeter(int pin) : 
Controller("P") {
  _pin = pin;
  _isOn = true;
  _alarm = false;
  _alarmSent = false;
  pinMode(_pin, INPUT);
}

void Perimeter::setCommand(String command) {
  _isOn = command.equalsIgnoreCase("ON");
  if(_isOn) {
    _alarm = false;
    _alarmSent = false;
  }
}

boolean Perimeter::check() {
  if(!_alarm) {
    _alarm = _isOn && digitalRead(_pin) == LOW;
  }
  if(_alarm) {
    if(_alarmSent) {
      return false;
    } 
    else {
      _alarmSent = true;
      return true;
    }
  }
  return false;
}

String Perimeter::getStatus() {
  return getId() + "(S=" + _isOn + ",A=" + _alarm + ")";
}

